<?php
class Model_CartHelper
{
	public static $NamespaceName="CartNamespace"; 
	public function getLastestCart(){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		return $cart->items;
	}	
	public function addNewItem($id, $name, $quantity, $price){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$locMapper=new Model_DishLocationMapper();
		
		$loc=$locMapper->getLocationByDishLocation($id);
		if ($loc!=null){
			$indexOfLoc=$this->indexOfLocation($loc-> getId());
			if ($indexOfLoc==-1){
				// not exist
				$this->addNewLoc($loc);
				$this->addNewDishToLocation($loc->getId(),$id,$name, $quantity, $price);
			}
			else{
				$this->addNewDishToLocation($loc->getId(),$id,$name, $quantity, $price);
			}
				
		}
	}
	
	private function addNewLoc(Model_Location $loc){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		if ($data==null)
		{
			$cart->items=Array();
		}
		$data=$cart->items;
		$item=Array
		(
				'LocId'=> $loc->getId(),
				'Name'=>$loc->getName(),
				'Address'=>$loc->getAddress(),
				'Phone'=>$loc->getPhone(),
				'Delivery'=>"0",
				'DeliveryAddress'=>"0",
				'items'=>Array()
		);
		array_push($data,$item);
		$cart->items=$data;
	}
	
	public function removeDish($disLocId, $locId){
		$indexOfLoc=$this->indexOfLocation($locId);
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		
		for ($i =0;$i<count($data[$indexOfLoc]['items']);$i++){
			if ($data[$indexOfLoc]['items'][$i]['Id']==$disLocId){
				unset($data[$indexOfLoc]['items'][$i]);
				$newArray=array_values($data[$indexOfLoc]['items']);
				$data[$indexOfLoc]['items']=$newArray;
			}
		}
		
		
		
		$cart->items=$data;
		return count($data[$indexOfLoc]['items']);
	}
	
	public function editDish($disLocId, $locId,$quantity){
		$indexOfLoc=$this->indexOfLocation($locId);
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
	
		for ($i =0;$i<count($data[$indexOfLoc]['items']);$i++){
			if ($data[$indexOfLoc]['items'][$i]['Id']==$disLocId){
				$data[$indexOfLoc]['items'][$i]['Quantity']=$quantity;
			}
		}
		$cart->items=$data;		
	}

	public function removeLoc($locId){
		$indexOfLoc=$this->indexOfLocation($locId);
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		
		unset($data[$indexOfLoc]);
		$newArray=array_values($data);
		$data=$newArray;
		
		$cart->items=$data;
	}
	
	public function updateDelivery($locId, $deliveryId){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		$index=$this->indexOfLocation($locId);
		$data[$index]['Delivery']=$deliveryId;
		$cart->items=$data;
	}
	
	public function calculateMoney(){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		$result=0;
		foreach ($data as $loc){
			$subtotal=0;
			$deliveryMapper=new Model_DeliveryMapper();
			$del=$deliveryMapper->getDelivery($loc['Delivery']);
			$delVal=$del['Fee'];
			if ($delVal==null){
				$delVal=0;
			}
			foreach ($loc['items'] as $item){
				$money=$item['Quantity']*$item['Price'];
				$subtotal=$subtotal+$money;
			}
			$subtotal=$subtotal+$delVal;
			$result=$result+$subtotal;
		}
		return $result;
		
	}
	
	public function WriteToDatabase(){
		
		try{
			$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
			$data= $cart->items;
			
			foreach($data as $loc){
				$total=0;
				$order=new Model_Order();
				$total=0;
				$locationId=$loc['LocId'];
				$deliveryId=$loc['Delivery'];
				$dateCreate = date('Y-m-d H:i:s');
				$customer=$this->getCustomerId();
				$_addressShip="";
				$order->setTotal($total)->setLocationId($locationId)->setDeliveryId($deliveryId)->setDateCreate($dateCreate);
				$order->setCustomerId($customer)->setAddressShip($_addressShip);
				$orderM=new Model_OrderMapper();
				$orderM->createOrder($order);
				if ($this->getOrderId($order)>=0){
					$order->setId($this->getOrderId($order));
				}else {
					$order->setId(-1);
				}
				foreach ($loc['items'] as $orderD){
					$orderDetail=new Model_OrderDetail();
					$orderDetail->setVat(0);
					$orderDetail->setSubtotal($orderD['Price']*$orderD['Quantity']);
					$total+= $orderD['Price']*$orderD['Quantity'];
					$orderDetail->setQuantity($orderD['Quantity']);
					$orderDetail->setOrderId($order->getId());
					$orderDetail->setDishLocationId($orderD['Id']);
					$orderDetail->setDiscount(0);
					
					$orderDetailM=new Model_OrderDetailMapper();
					$orderDetailM->createOrderDetail($orderDetail);
				}
				$this->updateOrderTotal($order->getId(), $total);
				return true;
			}
		}
		catch(Exception $e){
			return false;
		}
		
		
	}
	
	function updateOrderTotal($id, $total){
		$registry 	= Zend_Registry::getInstance();
		$DB = $registry['my_db'];
		$data = array('Total' => $total
		);
		$DB->update('order', $data,'OrdID ='.$id);
		
	}

	function getOrderId(Model_Order $order){
		$registry 	= Zend_Registry::getInstance();
		$DB = $registry['my_db'];
		$select=new Zend_Db_Select($DB);
		$select->from('order','*')->where(' LocID= ?', $order->getLocationId())
       ->where(' CusID= ?', $order->getCustomerId())->where(' DelID= ?', $order->getDeliveryId())
       ->where(' DateCreate= ?', $order->getDateCreate())
		;
		$data=$DB->fetchAll($select);
		if (count($data>0)){
			return $data[0]['OrdID'];
		}
		else{
			return -1;
		}
		
		
	}
	
	
	function getCustomerId(){
		
	}
	private function addNewDishToLocation($locId, $dishLocId,$name, $quantity, $price){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		if ($data==null)
		{
			$cart->items=Array();
		}
		$data=$cart->items; //array of locations
		$item=Array
		(
				'Id'=> $dishLocId,
				'Name'=>$name,
				'Quantity'=>$quantity,
				'Price'=>$price
		);
		$indexOfLoc=$this->indexOfLocation($locId);
		$indexOfDish=$this->indexOfItem($dishLocId, $locId);
		if ($indexOfDish>-1){
			//existed
			$data[$indexOfLoc]['items'][$indexOfDish]['Quantity']=$data[$indexOfLoc]['items'][$indexOfDish]['Quantity']+ $quantity;
			$cart->items=$data;
		}
		else
		{
			//add new
			array_push($data[$indexOfLoc]['items'], $item);
			$cart->items=$data;
		}
	}		
	public function clearCart(){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$cart->items=Array();
	}

	public function indexOfItem($id, $locId){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$indexofLoc=$this->indexOfLocation($locId);
		if ($indexofLoc==-1) return -1;
		$data=$cart->items[$indexofLoc];
		if ($data==null) return -1;
		for($i=0;$i<count($data['items']);$i++){
			$item=$data['items'][$i];
			if ($item['Id']==$id){
				return $i;
			}		
		}
		return -1;
	}
	
	public function indexOfLocation($locId){
		$cart = new Zend_Session_Namespace(Model_CartHelper::$NamespaceName);
		$data=$cart->items;
		if ($data==null) return -1;
		for($i=0;$i<count($data);$i++){
			$item=$data[$i];
			if ($item['LocId']==$locId){
				return $i;
			}
		}
		return -1;
	}
	
		
		
	
	
}